package com.yx.customized;


import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Build;
import android.os.Handler;
import android.support.v4.content.ContextCompat;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ProgressBar;

/**
 * Created by yx on 2017/6/2.
 * OW等待框，加载中loading...
 * 不能使用Application全局Context,Dialog不支持
 * 如：DialogWaitingUtil.showLoading(MyApplication.getInstance())崩溃！
 * 需传当前上下文，注意及时关闭
 */
public class DialogOwUtil {

    private static Dialog dialog;
    public static Context mContext;
    private static OWLoadingView ow;

    public static void showLoading(Context context) {
        View v = LayoutInflater.from(context).inflate(
                R.layout.dialog_ow_loading, null);
        ow = (OWLoadingView) v.findViewById(R.id.owloading);
        ow.setColor(ContextCompat.getColor(context, R.color.ow_loading));
        dialog = new AlertDialog.Builder(context, R.style.TranslucentNoBar).show();
        dialog.setOnKeyListener(xxx);
        // 触摸dialog外边时dialog是否消失
        dialog.setCanceledOnTouchOutside(false);
        dialog.setContentView(v);

        Window w = dialog.getWindow();
        WindowManager.LayoutParams lp = w.getAttributes();
        DisplayMetrics d = context.getResources().getDisplayMetrics(); // 获取屏幕宽、高用
        lp.width = (int) (d.widthPixels * 0.28); // 宽度设置为屏幕宽度的0.6--,全宽为1，最低是0
        //lp.height = (int) (d.heightPixels * 0.28); // 高度设置为屏幕的0.6
        lp.height = (int) (d.widthPixels * 0.28); // 高度设置为屏幕宽度的0.6
        // popupWindow.setWidth(display.widthPixels * 9 / 10);
        // popupWindow.setHeight(display.heightPixels * 9 / 10);
        // lp.alpha = 0.3f;
        w.setAttributes(lp);

        //启动动画方式1：
        //new Handler().postDelayed(new Runnable() {
        //    @Override
        //    public void run() {
        //        ow.startAnim();//启动OWLoading
        //    }
        //},200);
        //启动动画方式2：
        ow.setAutoStartAnim(true);//动画初始完毕直接开始动画

    }

    public static void hidn() {
        if (dialog != null && dialog.isShowing()) {
            dialog.dismiss();
            dialog.cancel();
            dialog = null;
            ow.stopAnim();
        }
    }

    private static DialogInterface.OnKeyListener xxx = new DialogInterface.OnKeyListener() {
        public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
            if (event.getAction() == KeyEvent.ACTION_DOWN) {
                if (keyCode == KeyEvent.KEYCODE_BACK) {// 按返回键对话框消失，透明度恢复 点击了手机返回键
                    try {
                        hidn();
                        ((Activity) mContext).finish();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            }
            return false;
        }
    };

}
